import { Component } from "react";
import PropTypes from "prop-types";
import Votemain from "./Votemain";
import Votefooter from "./Votefooter";

export default class Vote extends Component{
    static propTypes={//校验title格式
        title:PropTypes.string.isRequired
    }
    static defaultProps={
        title:"vue好学吗？"
    }
    state={
        supNum:6,
        oppNum:3
    }
    add=(type)=>{//this--->Vote类
        if(type=="sup"){
            this.setState({
                supNum:this.state.supNum+1
            })
        }else if(type=="opp"){
            this.setState({
                oppNum:this.state.oppNum+1
            })
        }
    }
    //祖先组件，制定一套传参规则
    static childContextTypes={
        supNum:PropTypes.number,
        oppNum:PropTypes.number,
        add:PropTypes.func
    }
    getChildContext(){//祖先组件，传递参数
       return{
          supNum:this.state.supNum,
          oppNum:this.state.oppNum,
          add:this.add
       }
    }
    render(){
        let {title}=this.props;
        let {supNum,oppNum}=this.state;
        return <div>
            <h1>{title}---总人数：{supNum+oppNum}</h1>
            <Votemain></Votemain>
            <Votefooter></Votefooter>
        </div>
    }
}